#include "threadpool.h"
#include <iostream>

// #define T 1

using uLong = unsigned long long;

class Mytask : public Task
{
public:
    Mytask(int begin, int end)
        : begin_(begin), end_(end)
    {
    }
    Any run()
    {
        uLong sum = 0;
        // std::cout << std::this_thread::get_id() << "begin" << std::endl;
        std::this_thread::sleep_for(std::chrono::seconds(3));
        for (int i = begin_; i <= end_; i++)
            sum += i;
        std::cout << std::this_thread::get_id() << " task :" << begin_ << std::endl;
        return sum;
    }

private:
    int begin_;
    int end_;
};

int main()
{

    {
        ThreadPool pool;
        pool.setMode(PoolMode::MODE_CACHED);
        pool.start();
        Result res1 = pool.submitTask(std::make_shared<Mytask>(1, 100000000));
        Result res2 = pool.submitTask(std::make_shared<Mytask>(1, 100000000));
        Result res3 = pool.submitTask(std::make_shared<Mytask>(1, 100000000));
        uLong sum1 = res1.get().cast_<uLong>();
        uLong sum2 = res2.get().cast_<uLong>();
        uLong sum3 = res3.get().cast_<uLong>();
        // pool.submitTask(std::make_shared<Mytask>(1, 100000000));
        // pool.submitTask(std::make_shared<Mytask>(2, 100000000));
        // pool.submitTask(std::make_shared<Mytask>(3, 100000000));
        // pool.submitTask(std::make_shared<Mytask>(4, 100000000));
        // pool.submitTask(std::make_shared<Mytask>(5, 100000000));
        std::cout <<"-----"<< (sum1 + sum2 + sum3) << std::endl;
    }
    std::cout << "main over" << std::endl;

#ifdef T
    {
        ThreadPool pool;
        pool.setMode(PoolMode::MODE_CACHED);

        pool.start(4);

        // std::this_thread::sleep_for(std::chrono::seconds(60));

        Result res2 = pool.submitTask(std::make_shared<Mytask>(100000001, 200000000));
        Result res3 = pool.submitTask(std::make_shared<Mytask>(200000001, 300000000));
        pool.submitTask(std::make_shared<Mytask>(200000001, 300000000));
        pool.submitTask(std::make_shared<Mytask>(200000001, 300000000));
        pool.submitTask(std::make_shared<Mytask>(1, 100000000));
        pool.submitTask(std::make_shared<Mytask>(1, 100000000));
        pool.submitTask(std::make_shared<Mytask>(1, 100000000));
        uLong sum1 = res1.get().cast_<uLong>();
        uLong sum2 = res2.get().cast_<uLong>();
        uLong sum3 = res3.get().cast_<uLong>();
        std::this_thread::sleep_for(std::chrono::seconds(3));
        std::cout << (sum1 + sum2 + sum3) << std::endl;
    }
    getchar();
#endif
}
